package com.situ.library.dao.impl;

import com.situ.library.dao.IStatusDao;
import com.situ.library.pojo.entity.Status;
import com.situ.library.pojo.vo.StatusCountVO;
import com.situ.library.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StatusDaoImpl implements IStatusDao {
    @Override
    public List<StatusCountVO> selectStatusCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<StatusCountVO> list = new ArrayList<>();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT s.`name`,COUNT(*) AS value\n" +
                    "FROM record as r INNER JOIN status as s\n" +
                    "ON r.status_id=s.id\n" +
                    "GROUP BY s.id";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int value = resultSet.getInt("value");
                StatusCountVO statusCountVO = new StatusCountVO();
                statusCountVO.setName(name);
                statusCountVO.setValue(value);
                list.add(statusCountVO);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }
}
